<template>
    <div class="content">
        <el-card shadow="never">
            <div class="form-1">
                <el-form :model="form" ref="form" label-width="100px" :inline="true">
                    <el-form-item label="订单名称：" prop="name">
                        <el-input placeholder="请输入订单名称" v-model="searchName" style="width: 250px" clearable size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="订单编号：" prop="code">
                        <el-input placeholder="请输入订单编号" v-model="searchCode" style="width: 250px" clearable size="small"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" plain @click="search" icon="el-icon-search" size="small">查询</el-button>
                        <el-button plain @click="reset" icon="el-icon-refresh-right" size="small">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-row class="btn">
                <el-button type="success" @click="add" size="small" icon="el-icon-plus" plain>新增</el-button>
                <el-button @click="refresh" size="small" icon="el-icon-refresh">刷新</el-button>
            </el-row>
            <el-dialog title="新增订单" :visible.sync="dialogAddVisible" width="35%">
                <el-form :model="addForm" ref="addForm" :rules="rules">
                    <el-form-item label="订单序号:" prop="id">
                        <el-input placeholder="请输入订单序号" v-model="addForm.id" style="width: 250px" size="small" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="订单名称:" prop="name">
                        <el-input placeholder="请输入订单名称" v-model="addForm.name" style="width: 250px" size="small" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="订单编号:" prop="code">
                        <el-input placeholder="请输入订单编号" v-model="addForm.code" style="width: 250px" size="small" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="授课讲师:" prop="lecturer">
                        <el-input placeholder="请输入授课讲师" v-model="addForm.lecturer" style="width: 250px" size="small" clearable></el-input>
                    </el-form-item>
                    <el-form-item align="center">    
                        <el-button type="primary" @click="save()" size="small">保存</el-button>
                        <el-button @click="dialogAddVisible = false" size="small">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
            <el-divider></el-divider>
            <el-table :data="pageData">
                <el-table-column label="序号" type="index" label-width="100px" align="center"></el-table-column>
                <el-table-column label="订单名称" prop="name" show-overflow-tooltip align="center"></el-table-column>
                <el-table-column label="订单编号" prop="code" show-overflow-tooltip align="center"></el-table-column>
                <el-table-column label="授课讲师" prop="lecturer" show-overflow-tooltip align="center"></el-table-column>
                <el-table-column label="操作" style="width: 200px;" align="center">
                    <template slot-scope="scope">
                        <el-button type="success" size="small" plain @click="showDetail(scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" plain @click="handleDel(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-row style="margin-top: 20px;">
                <el-col style="text-align: center">
                    <el-pagination
                        @size-change="handleSizeChange" 
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 50, 100]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                    </el-pagination>
                </el-col>
            </el-row>
        </el-card>
        <el-dialog title="订单管理" :visible.sync="dialogFormVisible" width="35%">
            <el-form :model="detailData" ref="detailData">
                <el-form-item label="订单序号:">
                    <el-input placeholder="请输入订单序号" v-model="detailData.id" style="width: 250px" size="small" clearable></el-input>
                </el-form-item>
                <el-form-item label="订单名称:">
                    <el-input placeholder="请输入订单名称" v-model="detailData.name" style="width: 250px" size="small" clearable></el-input>
                </el-form-item>
                <el-form-item label="订单编号:">
                    <el-input placeholder="请输入订单编号" v-model="detailData.code" style="width: 250px" size="small" clearable></el-input>
                </el-form-item>
                <el-form-item label="授课讲师:">
                    <el-input placeholder="请输入授课讲师" v-model="detailData.lecturer" style="width: 250px" size="small" clearable></el-input>
                </el-form-item>
                <el-form-item align="center">    
                    <el-button @click="dialogFormVisible = false" size="small">取消</el-button>
                    <el-button type="primary" @click="handleEdit()" size="small">保存</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    orderList,
    addOrder,
    updateOrder,
    deleteById,
    search
} from '@/api/order.js'
export default{
    data(){
        return{
            detailData: {},
            dialogFormVisible: false,
            dialogAddVisible: false,
            form:{
                name: '',
                code: ''
            },
            searchName: '',
            searchCode: '',
            orderData:[],
            addForm: {},
            //分页
            currentPage: 1,
            pageSize: 10,
            total: 0,
            pageData: [],  //分页后的数据
            rules:{
                id: [
                    {
                        required: true,
                        message: '请输入订单序号',
                        trigger: 'blur'
                    }
                ],
                name: [
                    {
                        required: true,
                        message: '请输入订单名称',
                        trigger: 'blur'
                    }
                ],
                code: [
                    {
                        required: true,
                        message: '请输入s订单编号',
                        trigger: 'blur'
                    }
                ],
                lecturer: [
                    {
                        required: true,
                        message: '请选择授课讲师',
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    mounted(){
        this.getOrderData()
    },
    methods: {
        getOrderData(){
            orderList()
            .then((res) => {
               if(res.data.code === 200) {
                    this.orderData = res.data.data; // 获取数据
                    this.total = this.orderData.length
                    this.getPageInfo() // 分页查询逻辑
               }else {
                   this.$message.error("订单列表获取失败")
               }
            }).catch(error => {
                this.$message.error("服务器连接失败")
            })
        },
        reset(){
            this.searchName = '',
            this.searchCode = ''
        },
        handleDel(id){
            this.$confirm("确认删除该订单?", "提示", {
                confirmButtonText: "确实",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                deleteById(id)
                .then(res => {
                    if(res.data.code === 200){
                        this.$message.success(res.data.message);
                        this.getOrderData()
                    }else{
                        this.$message.error("订单删除失败")
                    }
                }).catch(error => {
                    this.$message.error("服务器连接失败")
                })
            }).catch(() => {
                this.$message.info("已取消删除")
            })
        },
        add(){
            this.addForm = {}
            this.dialogAddVisible = true
        },
        save(){
            this.$confirm("确认保存该订单?", "提示", {
                confirmButtonText: "确实",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                addOrder(this.addForm)
                .then(res => {
                    if(res.data.code === 200){
                        this.$message.success(res.data.message)
                        this.getOrderData()
                        this.dialogAddVisible = false
                    }else{
                        this.$message.warning(res.data.message)
                    }
                }).catch(error => {
                    this.$message.error("服务器连接失败")
                })
            }).catch(() => {
                this.$message.info("已取消保存")
            })
        },
        showDetail(row){
            this.detailData = row
            this.dialogFormVisible = true
        },
        //分页
        handleCurrentChange(pageNumber){
            this,this.currentPage = pageNumber
            this.getPageInfo()
        },
        handleSizeChange(size){
            this.pageSize = size
            this.getPageInfo()
        },
        //获取当前页的信息
        getPageInfo(){
            this.pageData = []
            for(let i = (this.currentPage - 1) * this.pageSize; i < this.total; i++){
                this.pageData.push(this.orderData[i]) // 遍历数据并添加到pageData中
                if(this.pageData.length === this.pageSize){ // 判断是否到达一页的要求
                    break
                }
            }
        },
        //查询
        search(){
            search({name: this.searchName, code: this.searchCode})
            .then((res) => {
                    if(res.data.code === 200){
                        this.orderData = res.data.data
                        this.total = this.orderData.length
                        this.getPageInfo()
                    }else{
                        this.$message.warning("订单查询失败")
                    }
            }).catch(error => {
                this.$message.warning("服务器连接失败")
            })
        },
        refresh(){
            this.currentPage = 1
            this.getOrderData()
        },
        handleEdit(){
            this.$confirm("确定要保存所修改的内容", "提示", {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: 'warning'
            }).then(() => {
                updateOrder(this.detailData)
                .then((response) => {
                    if(response.data.code === 200){
                        this.$message.success("订单修改成功")
                        this.getOrderData()
                        this.dialogFormVisible = false
                    }else{
                        this.$message.warning("修改订单失败")
                        console.error('修改数据时响应异常:', response) 
                    }
                }).catch(error => [
                    this.$message.error("与服务器连接失败")
                ])
            }).catch(() => {
                this.$message.info("已取消编辑")
            })
        }
    }
}
</script>

<style>
.el-card, .form-1{
    margin-top: 20px;
}

.add{
    margin-left: 15px;
}

.el-table-column{
    text-align: center;
}

.btn{
    margin-left: 15px;
}
</style>